# import datetime.datetime
# from datetime import datetime
from flask import Blueprint, request
from conn_mysql import ConnectMysql
from .access_token_notify import access_token_verify_func

app_history = Blueprint('app_history', 'api')

@app_history.route('/app_history', methods=['POST'])
def history():

    user_id = request.form.get('user_id')
    access_token = request.form.get('access_token')

    print([user_id, access_token])

    if not all([user_id, access_token]):

        return {
            'status': -1,
            'errorCode': 61,
            'errorMsg': '请检查你提交的参数',
            'resultBody': None,
        }

    #令牌验证
    access_token_notify = access_token_verify_func(access_token, user_id)

    if access_token_notify["status"] == -1:

        return access_token_notify

    
    with ConnectMysql() as c:

        sql = "SELECT * FROM call_car WHERE user_id=%s"

        result = c.select_all(sql, (user_id,))

    for i in result:

        i['go_time'] = i['go_time'].__str__()
        i['regedate'] = i['regedate'].__str__()

    return {
        "status": 1,
        "errorCode": None,
        "errorMsg" : None,
        "resultBody": result,
    }